package org.springcloud.wisdom.spring_01_canteen.admin;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springcloud.wisdom.spring_01_canteen.login.entity.Mall;

import java.util.List;


@Mapper
public interface CanMalDao {
    //查看所有商家（根据类型和名字查）
    //分页查询
    //管理员审核商家
    @Select("<script>" +
            "SELECT * FROM wisdom.mall" +
            "<where>" +
            "   <if test=\"mrange != null\">" +
            "       mrange = #{mrange}" +
            "   </if>" +
            "   <if test=\"mname != null\">" +
            "<bind name=\"Key\" value=\"'%'+mname+'%'\"/>" +
            "                           mname like #{Key}" +
            "   </if>" +
            "</where>" +
            "ORDER BY CASE " +
            "           WHEN isdel = '未审核' THEN 0 " +
            "           ELSE 1 " +
            "          END, " +
            // 您可以添加其他排序条件，比如按 ID 或创建时间排序
            "         mid ASC " +
            "limit #{start},#{size}" +
            "</script>")
    List<Mall> canmall(int start, int size, String mrange, String mname);
    @Select("<script>" +
            "SELECT count(*) FROM wisdom.mall" +
            "  <where>" +
            "    <if test=\"mrange != null\">" +
            "      mrange = #{mrange}" +
            "    </if>" +
            "    <if test=\"mname != null\">" +
            " <bind name=\"Key\" value=\"'%'+mname+'%'\"/>"+
            "                        mname like #{Key} " +
            "    </if>" +
            "</where>"+
            "</script>")
    public int countOnres(String mrange,String mname);

    //管理员管理商家
    @Select("<script>" +
            "SELECT * FROM wisdom.mall" +
            "<where>" +
            "   <if test=\"mrange != null\">" +
            "       mrange = #{mrange}" +
            "   </if>" +
            "   <if test=\"mname != null\">" +
            "<bind name=\"Key\" value=\"'%'+mname+'%'\"/>" +
            "                           mname like #{Key}" +
            "   </if>" +
            "    and isdel = '已同意'" +
            "</where>" +
            "limit #{start},#{size}" +
            "</script>")
    List<Mall> canmall1(int start, int size, String mrange, String mname);
    @Select("<script>" +
            "SELECT count(*) FROM wisdom.mall" +
            "  <where>" +
            "    <if test=\"mrange != null\">" +
            "      mrange = #{mrange}" +
            "    </if>" +
            "    <if test=\"mname != null\">" +
            " <bind name=\"Key\" value=\"'%'+mname+'%'\"/>"+
            "                        mname like #{Key} " +
            "    </if>" +
            "    and isdel = '已同意'" +
            "</where>"+
            "</script>")
    public int countOnres1(String mrange,String mname);

    //学生查看商家（根据食堂查）
    @Select("<script>" +
            "SELECT * FROM wisdom.mall " +
            "<where>" +
            "    <if test=\"canteen != null and canteen.trim() != ''\">" +
            "        canteen like CONCAT('%', #{canteen}, '%')" +
            "    </if>" +
            "    and status = 1 " +
            "    and isdel = '已同意'" +
            "</where>" +
            "</script>")
    List<Mall> mall(String canteen);

    //修改商家信息
    @Update("<script>"
            + "update wisdom.mall"
            + "    <set>"
            + "        <if test='null!= mname'>"
            + "            mname = #{mname},"
            + "        </if>"
            + "        <if test='null!= mphone'>"
            + "            mphone = #{mphone},"
            + "        </if>"
            + "        <if test='null!= maddress'>"
            + "            maddress = #{maddress},"
            + "        </if>"
            + "        <if test='null!= mrange'>"
            + "            mrange = #{mrange},"
            + "        </if>"
            + "        <if test='null!= canteen'>"
            + "            canteen = #{canteen},"
            + "        </if>"+
            "            <if test=\"null!= status\">" +
            "                status =#{status}," +
            "            </if>"+
            "            <if test=\"null!= isdel\">" +
            "                isdel =#{isdel}," +
            "            </if>" +
             "    </set>"
            + "    where mid = #{mid}"
            + "</script>")
    public int updateMall(Mall mall);

    //修改商家状态
    @Update("UPDATE wisdom.mall SET status = #{status} WHERE mid = #{mid}")
    int updateStatus(@Param("mid") Integer mid,@Param("status") Integer status);

    //审核是否注册成功
    @Update("UPDATE wisdom.mall SET isdel = #{isdel} WHERE mid = #{mid}")
    int updateIsdel(@Param("mid") Integer mid,@Param("isdel") Integer isdel);
}
